//
//  AppDelegate.swift
//  sinaWeibo
//
//  Created by LSY on 15/12/12.
//  Copyright © 2015年 lsy. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
        let account = UserAccount.loadAccount()
//        不使用sb 加载window,就需要程序员手动加载
        
//        判断版本
//        let result = isNewVersion()
//        print(result)
//        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
        
//        添加跟控制器
        
//        设置主体颜色
        setThemeColor()
//        注册通知
        registerNotification()
        
        window?.rootViewController = defaultViewController()
        
        return true
    }

    
//    注册根控制器的通知
    
  private  func registerNotification(){
    
//    方法没写：号将提示找不到此方法的错误
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"switchRootVC:", name: WBSwitchRootVCNotification, object: nil)
    
    }
    
//    单例对象的方法，此方法写在appdelegate中只有单例对象被销毁的时候才调用此方法所以写与不写只是编码习惯的问题
   deinit{
        
    NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
//    为了兼容OCde 方法使用@objc
    //只要是OC中 target - action 这种消息机制 seletor类型都需要使用 @objc 来兼容
    //1.按钮的点击事件
    //2.通知的响应事件
    //3.代理的可选协议方法
 @objc private func switchRootVC (n:NSNotification) {
    
//    print(n)
        if n.object != nil{
        
        window?.rootViewController = WelcomeViewController()
            return
        }
    
   window?.rootViewController = MainViewController()
    
//    print(n)
    
    }
    
//    根据用户的登录判断控制器
    
    private func  defaultViewController() -> UIViewController {
    
        
        if AccountViewModel().userLogin {
        
            return isNewVersion() ? NewFeatureViewController() : WelcomeViewController()
        }
        
    return MainViewController()
    
    }
    
//    判断是否是新版本
    private func isNewVersion() -> Bool {

//   获取当前的版本号
    let info = NSBundle.mainBundle().infoDictionary
//        将版本号强转成string类型
     let currentStr = info!["CFBundleShortVersionString"] as!String
        let currentNum = Double(currentStr)
        
//        print(currentStr)
        
//      设置沙盒存储的属性
        let userDefault = NSUserDefaults.standardUserDefaults()
        let sandBoxKey = "sandBoxkey"
        let lastNum = userDefault.doubleForKey(sandBoxKey)
//        将版本号保存到沙盒
        userDefault.setDouble(currentNum!, forKey: sandBoxKey)
        userDefault.synchronize()
        
    return  currentNum > lastNum
    
    }
    
//    设置主题颜色
    func setThemeColor (){
    
    UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    
    }
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

